define(function(require, exports, module) {
    var ALL = require("../../common/all"),
        CONFIG = ALL.CONFIG,
        METHOD = ALL.METHOD,
        Dialog = ALL.Dialog,
        Init = ALL.Init;

    var _Dialog = new Dialog({
        title : "提示",
        content : "<p>是否确定添加/修改？</p>",
        text4Btn: ["确定","取消"],
        class4Btn: ["","line"]
    });

    var _vm = Init.initVue({
        data : {
            check: {},
            id : null,
            type : "",
            level : null,
            selected : false,
            team_id : null,
            colids : [],
            teamList : [],
            departList : [],
        },
        methods: {
            submit : function() {
                var obj,url,
                    flag,
                    arr = [];

                this.check.$valid();
                flag = this.check.flag;

                if (!flag) { return; }

                if (_vm.colids.length < 1) {
                    METHOD.alert("请设置藏品");
                    return;
                } else {
                    _vm.colids.forEach(function(v){
                        arr.push(v.col_id);
                    });
                }

                if (_vm.id) {
                    _Dialog.changeCfg({content : "<p>是否确定修改？</p>"});
                    url = "team/collection/modify";
                } else {
                    _Dialog.changeCfg({content : "<p>是否确定添加？</p>"});
                    url = "team/collection/create";
                }

                obj = {
                    team_id: _vm.team_id,
                    col_id: arr,
                    is_department_col: _vm.selected ? 1 : 0
                }

                _Dialog.showUI(function(i) {
                    if (i == 0) {
                        METHOD.loading.show();
                        METHOD.ajax(url,{data: JSON.stringify(obj)},{type : 'post'}).done(function(){
                            METHOD.loading.hide();
                            METHOD.alert("操作成功").done(function(){
                                window.location.href = "../team/assess.html";
                            });
                        }).fail(function(){
                            METHOD.loading.hide();
                        });
                    }
                    this.hideUI();
                });
            },
            remove : function(item, i){
               this.colids.splice(i, 1);
            },
            add : function() {
                this.colids.push({col_id:""});
            }
        }
    });

    METHOD.loading.show();
    METHOD.ajax("team/list",{}).done(function(json){
        METHOD.loading.hide();
        _vm.teamList = json.data.filter(function(v){
            if (v.level == 1) { return v; }
        });
        _vm.departList = json.data.filter(function(v){
            if (v.level == 2) { return v; }
        });
    }).fail(function(){
        METHOD.loading.hide();
    });

    _vm.id = METHOD.getQueryParms("id") || null;

    if (_vm.id) {
        METHOD.loading.show();
        METHOD.ajax("team/collection/detail",{id:_vm.id}).done(function(json){
            METHOD.loading.hide();
            _vm.team_id = json.data.team_id;
            _vm.level = json.data.level;
            _vm.colids = json.data.col;

        }).fail(function(){
            METHOD.loading.hide();
        })
    } else {
        _vm.colids.push({col_id:""});
    }

});

